package com.voicechat.firstmet.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.utils.ConstantSet;
import com.voicechat.firstmet.utils.ImageUtils;
import com.voicechat.firstmet.utils.SDCardUtils;
import com.voicechat.firstmet.utils.ScreenUtils;
import com.voicechat.firstmet.view.CuttingFrameView;
import com.voicechat.firstmet.view.PerfectControlImageView;

import java.io.IOException;

/**
 * 图片裁剪页面
 */
public class ClippingPageActivity extends AppCompatActivity {


    private static final Object TAG = "ClippingPageActivity";
    private PerfectControlImageView imageView;
    private CuttingFrameView cuttingFrameView;
    private Bitmap bitmap;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clipping_page);
        ScreenUtils.hideBottomUIMenu(ClippingPageActivity.this);
        initTitle();

        cuttingFrameView = (CuttingFrameView) findViewById(R.id.cutingFrame);
        imageView = (PerfectControlImageView) findViewById(R.id.targetImage);
        path = null;
        switch (getIntent().getStringExtra("type")) {
            case "takePicture":
                bitmap = ImageUtils.DecodLocalFileImage(ConstantSet.LOCALFILE + ConstantSet.USERTEMPPIC, this);
                path = ConstantSet.LOCALFILE + ConstantSet.USERTEMPPIC;
                break;
            case "selectPicture":
                path = getIntent().getStringExtra("path");
               // bitmap = ImageUtils.DecodLocalFileImage(path, this);
                bitmap = BitmapFactory.decodeFile(path);
                break;
        }
        if (bitmap != null) {
            //解决三星等手机获取系统照片自动横屏显示的问题
            if (path != null) {
                int degree = readPictureDegree(path);
                Bitmap bitmap1 = rotaingImageView(degree, bitmap);
                imageView.setImageBitmap(bitmap1);
            } else {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
    //获取图片的旋转角度
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }
    //然后对图片进行旋转：

    /**
     * 旋转图片
     *
     * @param angle
     * @param bitmap
     * @return Bitmap
     */
    public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();
        ;
        matrix.postRotate(angle);
        System.out.println("angle2=" + angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }


    /**
     * 初始化title
     */
    private void initTitle() {
        ImageView rightImage = (ImageView) findViewById(R.id.id_img_right);
        TextView title = (TextView) findViewById(R.id.id_title);
        TextView righttext = (TextView) findViewById(R.id.id_text_right);
        ImageView back = (ImageView) findViewById(R.id.id_back);
        rightImage.setVisibility(View.GONE);
        righttext.setVisibility(View.VISIBLE);
        title.setText("旋转");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ClippingPageActivity.this.finish();
            }
        });
//        title.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText( ClippingPageActivity.this,"旋转",0).show();
//                if (bitmap != null) {
//                   // Bitmap bitmap = imageView.rotaingImageView(90, ClippingPageActivity.this.bitmap);
//                    imageView.invalidate();
//                }
//
//            }
//        });
        righttext.setText("确定");
        righttext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog sp = new ProgressDialog(ClippingPageActivity.this);
                sp.show();
                Bitmap bitmap = cuttingFrameView.takeScreenShot(ClippingPageActivity.this);
                switch (getIntent().getStringExtra("type")) {
                    case "takePicture":
                        SDCardUtils.saveMyBitmap(ConstantSet.LOCALFILE, ConstantSet.USERINFO, bitmap);
                        break;
                    case "selectPicture":
                        SDCardUtils.saveMyBitmap(ConstantSet.LOCALFILE, ConstantSet.USERINFO, bitmap);
                        break;
                    case "takePicture2":
                        SDCardUtils.saveMyBitmap(ConstantSet.LOCALFILE, ConstantSet.USERPIC, bitmap);
                        break;
                    case "selectPicture2":
                        SDCardUtils.saveMyBitmap(ConstantSet.LOCALFILE, ConstantSet.USERPIC, bitmap);
                        break;
                    case "takePictureLogo":
                        SDCardUtils.saveMyBitmap(ConstantSet.LOCALFILE, ConstantSet.USERPICLOGO, bitmap);
                        break;
                    case "selectPictureLogo":
                        SDCardUtils.saveMyBitmap(ConstantSet.LOCALFILE, ConstantSet.USERPICLOGO, bitmap);
                        break;
                    case "first_takePicture":
                        SDCardUtils.saveMyBitmap(ConstantSet.LOCALFILE, ConstantSet.USERINFO, bitmap);
                        break;
                    case "first_selectPicture":
                        SDCardUtils.saveMyBitmap(ConstantSet.LOCALFILE, ConstantSet.USERINFO, bitmap);

                        break;
                    default:
                        break;
                }

//                ByteArrayOutputStream baos=new ByteArrayOutputStream();
//                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//                byte [] bitmapByte = baos.toByteArray();
//                it.putExtra("result", bitmapByte);
                sp.dismiss();
                Intent it = new Intent();
                setResult(RESULT_OK, it);
                ClippingPageActivity.this.finish();


            }
        });
    }


}
